package com.seata.core.config;

import io.seata.core.context.RootContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 蚂蚁会花呗
 * @date 2021/9/10 14:11
 */
@Component
public class SeataXidFilter  extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        String restXid = request.getHeader("xid");
        if (StringUtils.isNotBlank(restXid)) {
            RootContext.bind(restXid);
        }
        filterChain.doFilter(request, response);
    }
}
